package com.vxdata.utils.modules.utils.security;

/**
 * @ClassName: WatermarkUtils
 * @Description: 水印工具类 文字转水印
 * @Version: 1.0
 */
public class WatermarkUtils {
    public static int WATERMARK_POS_NONE = 0;
    public static int WATERMARK_POS_HEAD = 1;
    public static int WATERMARK_POS_TAIL = 2;

    /**
     * 将字符串转换成二进制字符串，以空格相隔
     *
     * @param input
     * @return
     */
    public static String strToBinary(String input) {
        char[] strChar = input.toCharArray();
        String result = "";
        String tmp = "";
        for (int i = 0; i < strChar.length; i++) {
            tmp = Integer.toBinaryString(strChar[i]);

            while (tmp.length() < 16) {
                tmp = "0" + tmp;
            }
            result += tmp;
            result += " ";
        }
        return result.trim();
    }

    /**
     * 将二进制字符串转换成int数组
     *
     * @param binStr
     * @return
     */
    public static int[] binstrToIntArray(String binStr) {
        char[] temp = binStr.toCharArray();
        int[] result = new int[temp.length];
        for (int i = 0; i < temp.length; i++) {
            result[i] = temp[i] - 48;
        }
        return result;
    }

    /**
     * 将二进制转换成字符
     *
     * @param binStr
     * @return
     */
    public static char binstrToChar(String binStr) {
        int[] temp = binstrToIntArray(binStr);
        int sum = 0;
        for (int i = 0; i < temp.length; i++) {
            sum += temp[temp.length - 1 - i] << i;
        }
        return (char) sum;
    }

    /**
     * 将以空格相隔的二进制转换为字符串
     *
     * @param input
     * @return
     */
    public static String binaryToStr(String input) {
        String[] tempStr = input.split(" ");
        char[] tempChar = new char[tempStr.length];
        for (int i = 0; i < tempStr.length; i++) {
            tempChar[i] = binstrToChar(tempStr[i]);
        }
        return String.valueOf(tempChar);
    }


    /**
     * 二进制字符串转换为零宽字符
     *
     * @param input
     * @return
     */
    public static String binaryToZeroWidth(String input) {
        String[] stringArray = input.split(" ");
        String result = "";
        for (int i = 0; i < stringArray.length; i++) {
            for (int j = 0; j < stringArray[i].length(); j++) {
                //数字转换
                int num = Integer.parseInt(stringArray[i].charAt(j) + "");
                if (num == 1) {
                    result += '\u200b'; // \u200b 零宽度字符（zero-width space）
                } else if (num == 0) {
                    result += '\u200e'; // \u200e 零宽度断字符（zero-width non-joiner）
                } else {
                    result += '\u200f'; // \u200f 零宽度连字符 (zero-width joiner)
                }
                result += '\ufeff'; // \ufeff 零宽度非断空格符 (zero width no-break space)
            }
        }
        return result;
    }

    /**
     * 零宽字符转二进制
     *
     * @param input
     * @return
     */
    public static String zeroWidthToBinary(String input) {
        String result = "";
        String[] binaryStr = input.split("\ufeff");
        for (int i = 0; i < binaryStr.length; i++) {
            if (binaryStr[i].equals("\u200B")) {
                result += "1";
            } else if (binaryStr[i].equals("\u200e")) {
                result += "0";
            }
            if ((i + 1) % 16 == 0) {
                result += " ";
            }
        }
        return result;
    }


    /**
     * 字符串编码
     *
     * @param input
     * @return
     */
    public static String encode(String input) {
        String binary = strToBinary(input);
        String result = binaryToZeroWidth(binary);
        return result;
    }

    /**
     * 零宽字符解码
     *
     * @param input
     * @return
     */
    public static String decode(String input) {
        String binary = zeroWidthToBinary(input);
        String result = binaryToStr(binary);
        return result;
    }

    /**
     * 添加水印数据
     *
     * @param src       源文件字符串
     * @param watermark 水印
     * @return 添加水印后的字符串
     */
    public static String addWatermark(String src, String watermark, int pos) {
        if (pos == WATERMARK_POS_HEAD) {
            return watermark + src;
        } else if (pos == WATERMARK_POS_TAIL) {
            return src + watermark;
        }
        return src;
    }


    /**
     * 提取水印数据
     *
     * @param input 添加水印的文本
     * @return
     */
    public static String extractWatermark(String input, int pos) {
        String watermark = "";
        if (pos == WATERMARK_POS_HEAD) {
            for (int i = 0; i < input.length(); i++) {
                if (input.charAt(i) != '\u200b' && input.charAt(i) != '\u200e' && input.charAt(i) != '\u200f' && input.charAt(i) != '\ufeff') {
                    watermark = input.substring(0, i);
                    break;
                }
            }
        } else if (pos == WATERMARK_POS_TAIL) {
            for (int i = input.length() - 1; i >= 0; i--) {
                if (input.charAt(i) != '\u200b' && input.charAt(i) != '\u200e' && input.charAt(i) != '\u200f' && input.charAt(i) != '\ufeff') {
                    watermark = input.substring(i + 1);
                    break;
                }
            }
        }
        return watermark;
    }


    public static void main(String[] args) throws Exception {
        // 文字转水印
        String data = "密文";
        String encodeData = encode(data);
        System.out.println("水印:  " + encodeData);
        String decodeData = decode(encodeData);
        System.out.println("水印解码:  " + decodeData);
    }

}
